<template>
  <b-modal
    :title="title"
    :width="640"
    :switchFullscreen="true"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          v-show="false"
          label="id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="showable"
            v-decorator="['id']" />
        </a-form-item>
        <a-form-item
          label="仓库编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="showable"
            v-decorator="['code', {rules: [{required: true, message: '请输入仓库编号'}]}]" />
        </a-form-item>
        <a-form-item
          label="仓库名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="showable"
            v-decorator="['name', {rules: [{required: true, message: '请输入仓库名称'}]}]" />
        </a-form-item>
        <a-form-item
          label="仓库地址"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-textarea
            :disabled="showable"
            v-decorator="['address', {rules: [{required: true, message: '请输入仓库地址'}]}]" />
        </a-form-item>
        <a-form-item
          label="仓库类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            :disabled="showable"
            :options="warehouseTypeList"
            v-decorator="['category', {rules: [{required: true, message: '请输入仓库类型'}]}]" />
        </a-form-item>
        <a-form-item
          label="负责人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="showable"
            v-decorator="['director', {rules: [{required: true, message: '请输入负责人'}]}]" />
        </a-form-item>
        <a-form-item
          label="参与可用量 "
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            :disabled="showable"
            :options="availableList"
            v-decorator="['availableNum', {rules: [{required: true, message: '请输入参与可用量'}]}]" />
        </a-form-item>
        <a-form-item
          label="库位管理 "
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            :disabled="showable"
            :options="manageList"
            v-decorator="['locationManagement', {rules: [{required: true, message: '请输入库位管理'}]}]" />
        </a-form-item>
        <a-form-item
          label="备注 "
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-textarea
            :disabled="showable"
            v-decorator="['remark', {rules: [{required: false, message: '请输入备注'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
    <template slot="footer" >
      <div v-show="editable || addable">
        <a-button key="cancel" @click="handleCancel">取消</a-button>
        <a-button key="forward" :loading="confirmLoading" type="primary" @click="handleOk">保存</a-button>
      </div>
    </template>
  </b-modal>
</template>

<script>
import { FormMixin } from '@/mixins/FormMixin'
import pick from 'lodash.pick'
import {dictItemsDropDown} from '@/api/sys/sys'
import {addObj,getObj,putObj} from '@/api/mdm/warehouseBase'
import {manageList, availableList } from './warehouseBaseTemplate'
import BModal from '@comp/Modal/BModal'
export default {
  name: 'WarehouseAddOrUpdate',
  components: { BModal },
  mixins: [FormMixin],
  data () {
    return {
      confirmLoading: false,
      warehouseTypeList: [],
      manageList,
      availableList,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    edit (id, type) {
      this.visible = true
      dictItemsDropDown('warehouse_type').then(res=>{
        this.warehouseTypeList = res.result
      })
      console.log('this.warehouseTypeList ',this.warehouseTypeList )
      const { form: { setFieldsValue, resetFields } } = this
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getObj(id).then(res => {
          const record = res.data
          this.confirmLoading = false
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['id', 'code', 'name', 'address', 'category', 'director', 'availableNum', 'locationManagement', 'remark']))
          })
        })
      } else {
        resetFields()
      }
    },
    //点击保存，执行的方法
    handleOk () {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          this.confirmLoading = true
          try {
            if (this.type === 'add') {
              await addObj(values)
            } else if (this.type === 'edit') {
              await putObj(values)
            }
            setTimeout(() => {
              this.confirmLoading = false
              this.$emit('ok')
              this.handleCancel()
            }, 1500)
          }catch (e){
            this.confirmLoading = false
            this.$message.error(e.msg)
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>